<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="企业简介" prop="brief">
        <el-input v-model="queryParams.brief" placeholder="请输入企业简介" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['dashboard:enterprise:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dashboard:enterprise:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dashboard:enterprise:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['dashboard:enterprise:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterpriseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="企业ID" align="center" prop="id" /> -->
      <el-table-column label="企业名称" align="center" prop="name" />
      <el-table-column label="企业简介" align="center" prop="brief">
        <template #default="scope">
          <!-- <span v-html="scope.row.content"></span> -->
          <el-button @click="openDetail(scope.$index)">查看详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="企业风貌" align="center" prop="imgArray" width="100">
        <template #default="scope">
          <image-preview v-for="item in scope.row.imgArray" :key="item" :src="item" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dashboard:enterprise:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dashboard:enterprise:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业管理对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body :zIndex="100">
      <el-form ref="enterpriseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业简介" prop="brief">
          <editor v-model="form.brief"></editor>
        </el-form-item>
        <el-form-item label="企业风貌" prop="imgArray">
          <image-upload v-model="form.imgArray" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-divider content-position="center">产品管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddTeaProduct">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteTeaProduct">删除</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="teaProductList"
          :row-class-name="rowTeaProductIndex"
          @selection-change="handleTeaProductSelectionChange"
          ref="teaProduct"
        >
          <el-table-column type="selection" width="50" align="center" />
          <!-- <el-table-column label="序号" align="center" prop="index" width="50" /> -->
          <!-- <el-table-column label="产品名称" prop="name" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.name" placeholder="请输入产品名称" />
            </template>
          </el-table-column> -->
          <el-table-column label="产品封面" prop="cover" width="200">
            <template #default="scope">
              <image-upload v-model="scope.row.cover" :limit="1" />
            </template>
          </el-table-column>
          <el-table-column label="产品价格" prop="price" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.price" placeholder="请输入产品价格" />
            </template>
          </el-table-column>
          <el-table-column label="显示顺序" prop="orderNum" width="200">
            <template #default="scope">
              <el-input-number v-model="scope.row.orderNum" controls-position="right" :min="0" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="企业简介详情" width="500px" v-model="detail">
      <div v-html="enterpriseList[nowIndex].brief"></div>
    </el-dialog>
  </div>
</template>

<script setup name="Enterprise">
import {
  listEnterprise,
  getEnterprise,
  delEnterprise,
  addEnterprise,
  updateEnterprise
} from "@/api/dashboard/enterprise"

const { proxy } = getCurrentInstance()

const enterpriseList = ref([])
const teaProductList = ref([])
const open = ref(false)
const loading = ref(true)
const nowIndex = ref(0)
const detail = ref(false)
const showSearch = ref(true)
const ids = ref([])
const checkedTeaProduct = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    brief: null
  },
  rules: {
    name: [{ required: true, message: "企业名称不能为空", trigger: "blur" }],
    orderNum: [{ required: true, message: "显示顺序不能为空", trigger: "blur" }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询企业管理列表 */
function getList() {
  loading.value = true
  listEnterprise(queryParams.value).then((response) => {
    enterpriseList.value = response.rows
    total.value = response.total
    loading.value = false
    enterpriseList.value.forEach((item) => {
      if (item.imgArray != "" && item.imgArray != null) item.imgArray = item.imgArray.split(",")
    })
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    brief: null,
    imgArray: null,
    orderNum: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  teaProductList.value = []
  proxy.resetForm("enterpriseRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加企业管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getEnterprise(_id).then((response) => {
    form.value = response.data
    if (form.value.imgArray != "" && form.value.imgArray != null) form.value.imgArray = form.value.imgArray.split(",")
    teaProductList.value = response.data.teaProductList
    open.value = true
    title.value = "修改企业管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["enterpriseRef"].validate((valid) => {
    if (valid) {
      form.value.teaProductList = teaProductList.value
      let post = {
        ...form.value
      }
      if (post.imgArray != null) {
        post.imgArray = Array(post.imgArray).join(",")
      }
      if (post.id != null) {
        updateEnterprise(post).then((response) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addEnterprise(post).then((response) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除企业管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delEnterprise(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
    .catch(() => {})
}

/** 产品管理序号 */
function rowTeaProductIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 产品管理添加按钮操作 */
function handleAddTeaProduct() {
  let obj = {}
  obj.name = ""
  obj.cover = ""
  obj.price = ""
  obj.orderNum = ""
  teaProductList.value.push(obj)
}

/** 产品管理删除按钮操作 */
function handleDeleteTeaProduct() {
  if (checkedTeaProduct.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的产品管理数据")
  } else {
    const teaProducts = teaProductList.value
    const checkedTeaProducts = checkedTeaProduct.value
    teaProductList.value = teaProducts.filter(function (item) {
      return checkedTeaProducts.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handleTeaProductSelectionChange(selection) {
  checkedTeaProduct.value = selection.map((item) => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "dashboard/enterprise/export",
    {
      ...queryParams.value
    },
    `enterprise_${new Date().getTime()}.xlsx`
  )
}

/** 打开详情 */
function openDetail(index) {
  nowIndex.value = index
  detail.value = true
}

getList()
</script>
